package com.gm.wholesale.module.good.ui.mainGood;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.gm.commonlib.base.baseBean.BaseListResp;
import com.gm.commonlib.base.baseui.baseFragment.BaseFragment;
import com.gm.commonlib.base.baseui.baseListviewUI.BaseGridFragment;
import com.gm.commonlib.module.evenbus.EventMessage;
import com.gm.commonlib.network.BaseHttpObserver;
import com.gm.wholesale.R;
import com.gm.wholesale.module.evenbus.EventBusType;
import com.gm.wholesale.module.config.ConfigData;
import com.gm.wholesale.module.good.model.GoodModel;
import com.gm.wholesale.module.kind.bean.KindBean;

import java.util.ArrayList;
import java.util.List;

import q.rorbin.verticaltablayout.VerticalTabLayout;
import q.rorbin.verticaltablayout.adapter.TabAdapter;
import q.rorbin.verticaltablayout.widget.ITabView;
import q.rorbin.verticaltablayout.widget.QTabView;
import q.rorbin.verticaltablayout.widget.TabView;

public class GoodMainFragment extends BaseFragment {


    VerticalTabLayout verticalTabLayout;

    ViewPager viewPager;
    List<KindBean> kindBeans = new ArrayList<>();

    private ArrayList<Fragment> mFragmentsList;


    @Override
    protected void onCreateContentBody(ViewGroup parent) {
        super.onCreateContentBody(parent);
        if (!ConfigData.getConfigData().hasCreateOrderPermission()) { //没有开单权限，进入订单管理界面
            return;
        }
        View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_content_right, parent);
        //todo 跑monkey时候 VerticalTabLayout这个三方库报空指针，只有这里处理下
        try {
            initView(contentView);
            getKinds();
        }
        catch (Exception e){

        }

    }

    void initView(View contentView) {
        viewPager = contentView.findViewById(R.id.viewPager);
        verticalTabLayout = contentView.findViewById(R.id.verticalTabLayout);

       // verticalTabLayout.setTabHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        verticalTabLayout.addOnTabSelectedListener(new VerticalTabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabView tab, int position) {
                for (int i = 0; i < verticalTabLayout.getTabCount(); i++) {
                    TabView tabView = verticalTabLayout.getTabAt(i);
                    TextView textView = verticalTabLayout.getTabAt(i).getTitleView();
                    if (tabView == tab) {
                        textView.setTextColor(getResources().getColor(R.color.white));
                        tab.getTabView().setBackgroundColor(getResources().getColor(R.color.color_06C160));
                    } else {
                        textView.setTextColor(getResources().getColor(R.color.color_666));
                        tabView.getTabView().setBackgroundColor(getResources().getColor(R.color.color_DBDEE6));
                    }

                }
            }

            @Override
            public void onTabReselected(TabView tab, int position) {

            }
        });


    }

    public class MyPagerAdapter extends FragmentStatePagerAdapter implements TabAdapter{
        public MyPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return mFragmentsList.get(position);
        }


        @Override
        public int getCount() {
            return kindBeans.size();
        }

        @Override
        public ITabView.TabBadge getBadge(int position) {

            return null;
        }

        @Override
        public ITabView.TabIcon getIcon(int position) {
            return null;
        }

        @Override
        public ITabView.TabTitle getTitle(int position) {

            return new QTabView.TabTitle.Builder()
                   .setTextColor(getResources().getColor(R.color.white),getResources().getColor(R.color.color_666))
                    .setTextSize(22)
                    .setContent(kindBeans.get(position).getName())
                    .build();
        }

        @Override
        public int getBackground(int position) {
            if (position==0){
                return R.color.color_06C160;
            }
            return 0;

        }


        @Override
        public CharSequence getPageTitle(int position) {
            return kindBeans.get(position).getName();

        }
    }


    private void getKinds() {
        addDefault();
        //是否基于sku筛选，0表示展示全部分类，1表示展示仅有商品的分类
        GoodModel.getCategory(this, 1, new BaseHttpObserver<>() {
            @Override
            protected void handleResultOkResp(BaseListResp<KindBean> resp) {

                 kindBeans.addAll(resp.getDatas());
                for (KindBean kindBean : kindBeans) {
                    if (kindBean==null || kindBean.getId()==0){
                       continue;
                    }
                    GoodContentFragment goodContentFragment = new GoodContentFragment();
                    goodContentFragment.setKindId(kindBean.getId());
                    goodContentFragment.setIsGetData(true);
                    mFragmentsList.add(goodContentFragment);
                }
               MyPagerAdapter  adapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
                viewPager.setAdapter(adapter);
                verticalTabLayout.setupWithViewPager(viewPager);

            }
        });


    }
    private void addDefault(){
        if (mFragmentsList!=null){
            mFragmentsList.clear();
            mFragmentsList=null;
        }
        mFragmentsList = new ArrayList<>();//添加碎片
        kindBeans.clear();
        KindBean kindBeanAl=new KindBean(0,"全部商品");
        kindBeans.add(kindBeanAl);
        GoodContentFragment goodContentFragment = new GoodContentFragment();
        goodContentFragment.setKindId(0);
        goodContentFragment.setIsGetData(true);
        mFragmentsList.add(goodContentFragment);
      //  mAdapter.notifyDataSetChanged();

    }

    public void updateGood(){
        if (mFragmentsList==null)return;
        for (Fragment fragment:mFragmentsList){
            BaseGridFragment baseGridFragment=((BaseGridFragment)fragment);
            if (baseGridFragment!=null && baseGridFragment.isCreateViewFinish()){
                baseGridFragment.refreshData();
            }

        }
    }

    @Override
    public void onReceiveEvent(EventMessage event) {
        if (event==null) return;
        super.onReceiveEvent(event);
        if (event.getCode()== EventBusType.UPDATE_KIND.getType()){
            updateKind();
        }
        else if (event.getCode()==EventBusType.UPDATE_GOOD_LIST.getType()){
            updateKind();
        }

    }

    public void updateKind(){
        getKinds();
    }



}
